package com.thenmorethings.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

public class LoginFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/*
	 * 登录过滤器
	 * (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {

			HttpServletRequest request = (HttpServletRequest)servletRequest;
			HttpServletResponse response = (HttpServletResponse)servletResponse;
			
			
			String uri = request.getRequestURI();
			System.out.println(uri);
			//判断当前请求地址是否是登录界面
			if(uri.contains("/admin/"))
			{
				//非登录请求
				if(request.getSession().getAttribute("loginSession") != null )
				{
					//说明已经登录过 放行
					chain.doFilter(request, response);
				}
				else
				{
					//没有登录，跳转到登陆界面
					response.sendRedirect(request.getContextPath() + "/admin/admin_loginUI.action");
				}
			}
			else
			{
				//放行
				chain.doFilter(request, response);
			}
			
	
	
	
	}

	
	
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
